package com.oa.co.controller;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.github.pagehelper.PageInfo;
import com.oa.co.bo.PerformanceBo;
import com.oa.co.bo.Reissue_cardBo;
import com.oa.co.common.PageVo;
import com.oa.co.common.R;
import com.oa.co.listener.WebPerformanceListener;
import com.oa.co.listener.WebRepairListener;
import com.oa.co.pojo.Performance;
import com.oa.co.pojo.Reissue_card;
import com.oa.co.service.RepairService;
import com.oa.co.vo.PerformanceBarVo;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    @Autowired
    private WebRepairListener webRepairListener;

    @RequestMapping("/repairList")
    public PageVo<Reissue_card> repairList(String applicant, String subordinate_department, Integer page, Integer limit){

        PageVo<Reissue_card> pageVo = new PageVo<>();
        PageInfo<Reissue_card> pageData =  repairService.selectRepairList(page,limit,applicant,subordinate_department);
        pageVo.setCode(0);

        pageVo.setCount(pageData.getTotal());
        pageVo.setData(pageData.getList());
        pageVo.setMsg("绩效分页数据");

        return pageVo;
    }

    @RequestMapping("/delrapair")
    public R delRapair(Integer id){

        int num = repairService.delRapair(id);
        if(num == 1){
            return  R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @RequestMapping("/repairEdit")
    public R repairEdit(Reissue_card reissue_card){
       int num =  repairService.repairEdit(reissue_card);
        if (num ==  1){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @RequestMapping("/repairAdd")
    public R repairAdd(Reissue_card reissue_card){
        reissue_card.setD_id("BK-"+System.currentTimeMillis());
        reissue_card.setCreation_time(new Date());
        int num =  repairService.repairAdd(reissue_card);
        if (num ==  1){
            return R.ok();
        }else {
            return R.error();
        }
    }

   //delAllRepair
   @RequestMapping("/delAllRepair/{ids}")
   public R delAllPerformance(@PathVariable("ids") String ids){
       String substring = ids.substring(1, ids.length() - 1);
       String[] split = substring.split(",");
       List<Integer> list = new ArrayList<>();
       for (String s:split
       ) {
           int i = Integer.parseInt(s);
           list.add(i);
       }
       int num = repairService.delAllRepair(list);
       for (Integer i: list
       ) {
           System.out.println(i);
       }
       if(num != 0){
           return R.ok();
       }else {
           return R.error();
       }
   }

   //bar
   @RequestMapping("/bar")
   public R bar(){
       List<PerformanceBarVo> list = repairService.repairBarData();
       return R.ok().data("setBar",list);
   }
   //pie
    @RequestMapping("/pie")
    public R pie(){
        List<PerformanceBarVo> list = repairService.repairPieData();
        return R.ok().data("setPie",list);
    }

    //import
    @RequestMapping("/import")
    @ResponseBody
    public R importFile(MultipartFile file){
        System.out.println("进入import");

        try {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), Reissue_cardBo.class, webRepairListener);

            ExcelReaderSheetBuilder sheet = read.sheet();

            sheet.doRead();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok();
    }

    @RequestMapping("/export")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response, Integer page, Integer limit, String applicant, String subordinate_department) throws IOException {
        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");


        //防止中文乱码
        String filename = URLEncoder.encode("补卡信息导出", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, Reissue_cardBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();

        //准备列表数据
        PageInfo<Reissue_card> pageData = repairService.selectRepairList(page,limit,applicant,subordinate_department);

        sheet.doWrite(pageData.getList());
    }

    @RequestMapping("/downloadTemplate")
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载 excel  模板文件
        //设置下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 得到这个文件的真实路径
        String realPath = request.getServletContext().getRealPath("/BKtemplate.xlsx");;

        File file = new File(realPath);
        //借助 commons-io 的 工具类 把 file 对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);

        //防止中文乱码
        String filename = URLEncoder.encode("补卡信息模板", "utf-8");

        //告诉浏览器  你要下载文件
        response.setHeader("Content-Disposition","attachment;filename="+filename+".xlsx");

        //得到一个 指向浏览器的输出流
        ServletOutputStream outputStream = response.getOutputStream();

        //输出流 把字节数组 输出到 浏览器
        outputStream.write(bytes);
        //关闭流
        outputStream.close();

    }
}
